/**
 * Project Name:mauto-web
 * File Name:ApiEventController.java
 * Package Name:com.inftech.mauto.core.web.front
 * Date:2017年3月30日下午6:00:11
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.web.front;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.constant.SessionConstEnum;
import com.inftech.mauto.core.service.ActivityConditionService;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * ClassName:ApiActivityConditionController <br/>
 * Function: 活动情况接口. <br/>
 * Date: 2017年3月30日 下午6:00:11 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/api/activityCondition")
public class ApiActivityConditionController extends BaseController {

	/**
	 * 注入活动情况业务层
	 */
	@Autowired
	protected ActivityConditionService activityConditionService;

	/**
	 * 
	 * save:新增带参数的二维码. <br/>
	 *
	 * @author puyf
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String save(HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			activityConditionService.saveQrcode(people);
			LOG.info("生成二维码成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("生成二维码失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * detail:获取活动情况详情. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/detail/{activityId}", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String detail(@PathVariable int activityId, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("activity_id", activityId);
			paramMap.put("people_id", people.getPeopleId());
			Map<String, Object> activityConditionMap = activityConditionService.queryActivityCondition(paramMap);
			LOG.info("获取活动情况详情成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), activityConditionMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("获取活动情况详情失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * receivePrize:领奖. <br/>
	 *
	 * @author puyf
	 * @param deliveryInfo
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/receivePrize", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public synchronized String receivePrize(@RequestBody String requestStr, HttpServletRequest request)
			throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 获取用户对象
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			// 用户id
			paramMap.put("peopleId", people.getPeopleId());
			// 联系人
			paramMap.put("deliveryContact", jsonObject.getString("deliveryContact"));
			// 联系电话
			paramMap.put("deliveryContactPhone", jsonObject.getString("deliveryContactPhone"));
			// 收货地址
			paramMap.put("deliveryAddress", jsonObject.getString("deliveryAddress"));
			// 活动情况id
			paramMap.put("activityCondId", jsonObject.getInteger("activityCondId"));
			activityConditionService.modifyActivityConditionReceive(paramMap);
			LOG.info("领奖成功");
			// 返回结果
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("领奖失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

}
